/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.beans.bind;

import java.util.Map;

/**
 * Provides a bean binder that uses a field property mapping handler to determine
 * the relationship between bean fields and business object properties. 
 * 
 * @author Scott Rossillo
 *
 */
public class SimpleBeanBinder extends AbstractBeanBinder implements BeanBinder {
	
	private FieldPropertyMappingHandler fieldPropertyMappingHandler;
	
	/**
	 * Creates a new simple bean binder.
	 */
	public SimpleBeanBinder() { }
	
	/**
	 * Creates a new simple bean binder that will use the given 
	 * field property mapping handler to map business object properties
	 * to bean fields.
	 * 
	 * @param fieldPropertyMappingHandler the <code>FieldPropertyMappingHandler</code>
	 * this binder will use to map business object properties
	 * to bean fields
	 */
	public SimpleBeanBinder(FieldPropertyMappingHandler fieldPropertyMappingHandler) {
		this.fieldPropertyMappingHandler = fieldPropertyMappingHandler;
	}

	/**
	 * Always returns true.
	 * 
	 * @see net.martinimix.beans.bind.BeanBinder#supports(java.lang.Class)
	 */
	public boolean supports(Class clazz) {
		return true;
	}

	/* (non-Javadoc)
	 * @see net.martinimix.beans.bind.AbstractBeanDataBinder#propertyToFieldNameMap(java.lang.Object)
	 */
	protected Map propertyToFieldNameMap(Object bean) {
		return fieldPropertyMappingHandler.mapPropertiesToFields(bean);
	}

	
	/**
	 * Sets the field property mapping handler to use to map business 
	 * object properties to bean fields.
	 * 
	 * @param fieldPropertyMappingHandler the <code>FieldPropertyMappingHandler</code>
	 * this binder will use to map business object properties
	 * to bean fields
	 */
	public void setFieldPropertyMappingHandler(
			FieldPropertyMappingHandler fieldPropertyMappingHandler) {
		this.fieldPropertyMappingHandler = fieldPropertyMappingHandler;
	}

}
